local Msg = require("core.Msg")
local RoleDBLogic = require("role.RoleDBLogic")
local Human = require("core.Human")
local InnerDataManagerFW = require("innerdata.InnerDataManagerFW")
local InnerDataDefine = require("innerdata.Define")
local RoleFFI = require("role.RoleFFI")
local FamilyManager = require("family.FamilyManager")
local ItemDefine = require("bag.ItemDefine")
local Lang = require("common.Lang")
local Broadcast = require("broadcast.Broadcast")
local Config = require("Config")

-- smeltList = {["uuid"] = {[1] = uuid, [1] = helpUuid, [2] = helpUuid}}
smeltList = smeltList or {} --元宝熔炼列表

--查询别人
function CG_SEARCH_OTHER(human, msg)
--  if human._id == msg.uuid then
--    return
--  end
  local db = {}
  db = RoleDBLogic.getDbByUuid(msg.uuid, {bag = 0,store = 0, cooldown =0, familyApplyList =0,familyShopList=0,mysteryshopList = 0,prestigeshopList=0})
  if not db then
    return
  end
  if db.bag then
    db.bag = nil
    db.store = nil 
    db.cooldown = nil 
    db.familyApplyList = nil
    db.familyShopList = nil
    db.mysteryshopList = nil
    db.prestigeshopList = nil
    db.taskRecord = nil
    db.mysteryPrestigeShopList = nil
    db.mysteryHonorShopList = nil
    db.cowryRecord = nil
    db.actValueRecord = nil
  end
  db.official = FamilyManager.getMemberOfficial(db.familyUuid, db._id) or -1
  if not Config.IS_MIDDLE then --跨服时，用已保存的值
    db.familyLv = FamilyManager.getFamilyLvByFamilyUuid(db.familyUuid)
    db.familyBanner = FamilyManager.getFamilyBannerByFamilyUuid(db.familyUuid)
  end
  db.type = msg.type
  InnerDataManagerFW.Send2LInnerData(human, InnerDataDefine.INNERDATA_TYPE_SEARCH_OHTHER, db)
end

function CG_SMELT_CANCEL_HELP(human, msg)
  cancelSmelt(human, msg.uuid)
end

function invite(human)
  if smeltList[human._id] then return end 
  smeltList[human._id] = {}
  smeltList[human._id][1] = {}
  smeltList[human._id][1].helpName = human.name
  smeltList[human._id][1].helpUuid = human._id
  smeltList[human._id][1].helpHead = human.db.sex
  smeltList[human._id][1].type = 0
end
--发送熔炼信息
function sendSmeltInfo(uuid, smeltType, uuid1)
  local sendMsg = Msg.gc.GC_SMELT_HELP_OTHER
  local fds = {}
  local len = 0
  sendMsg.isComplete = smeltType
--  sendMsg.list[0] = #smeltList[uuid]
  for i=1, #smeltList[uuid] do
    local human = Human.onlineUuid[smeltList[uuid][i].helpUuid]
    if human and uuid1 ~= smeltList[uuid][i].helpUuid then
      len = len + 1
      fds[len] = human.fd 
      sendMsg.list[i].name = smeltList[uuid][i].helpName
      sendMsg.list[i].uuid = smeltList[uuid][i].helpUuid
      sendMsg.list[i].head = smeltList[uuid][i].helpHead
      sendMsg.list[i].type = smeltList[uuid][i].type
   end
  end
  sendMsg.list[0] = len
  if smeltType == 2 then sendMsg.list[0] = 0 end
  fds[0] = len
  Msg.sendMulti(sendMsg, fds)
end

function checkCanHelp(human, uuid)
  for i=1, #smeltList[uuid] do
    if RoleFFI.ffi_string(human.db.helpUuid) ~= "" then
      Broadcast.sendBroadcast(human, Lang.DOWN, Lang.RMB_SMELT_HAVE_AlREADY_HELP)
      return false
    end
    if smeltList[human._id] then
      Broadcast.sendBroadcast(human, Lang.DOWN, Lang.RMB_SMELT_ALREADY_DO)
      return false
    end
  end
  return true
end
--帮助他人熔炼
function helpOther(human, oType, info)
  if not info or not info.uuid then
    return 
  end
  if not smeltList[info.uuid] then
    return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.RMB_SMELT_HAVE_SUCCESS)
  end
  if not checkCanHelp(human, info.uuid) then
    return
  end
  local len = #smeltList[info.uuid]
  if len >= ItemDefine.SMELT_MAX_HELP_PEOPLE + 1 then
    return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.RMB_SMELT_HELP_PEOPLE_MAX)
  end
  human.db.helpUuid = info.uuid
  len = len + 1
  smeltList[info.uuid][len] = {}
  smeltList[info.uuid][len].helpName = info.helpName
  smeltList[info.uuid][len].helpUuid = info.helpUuid
  smeltList[info.uuid][len].helpHead = info.helpHead
  smeltList[info.uuid][len].type = 1
  sendSmeltInfo(info.uuid, 0)
end
--取消帮助
function cancelSmelt(human, uuid)
  if not uuid then
    uuid = RoleFFI.ffi_string(human.db.helpUuid)
  end 
  if not smeltList[uuid] then return end
  local index = 0
  for i=2, #smeltList[uuid] do
    if smeltList[uuid][i].helpUuid == human._id then
      index = i
      break
    end
  end
  if #smeltList[uuid] == index then
    smeltList[uuid][index] = nil
  else
    for i=2, #smeltList[uuid] - 1 do
      if i >= index then
        smeltList[uuid][i].helpName = smeltList[uuid][i + 1].helpName
        smeltList[uuid][i].helpUuid = smeltList[uuid][i + 1].helpUuid
        smeltList[uuid][i].helpHead = smeltList[uuid][i + 1].helpHead
        smeltList[uuid][i].type     = smeltList[uuid][i + 1].type        
      end
    end
    smeltList[uuid][#smeltList[uuid]] = nil
  end
  human.db.helpUuid = ""
  Broadcast.sendBroadcast(human, Lang.DOWN, Lang.RMB_SMELT_HELP_CACLE_SUCCESS)
  sendSmeltInfo(uuid, 0, (not uuid) and human._id)
  if uuid then
    local sendMsg = Msg.gc.GC_SMELT_HELP_OTHER
    sendMsg.isComplete = 0
    sendMsg.list[0] = 0
    Msg.send(sendMsg, human.fd)
  end
end
--给帮助的人增加绑定元宝
function smeltHelpSuccess(uuid)  
  for i=2, #smeltList[uuid] do
    local helper = Human.onlineUuid[smeltList[uuid][i].helpUuid]
    if helper then
      --增加绑定元宝
      local infoW = {}
      infoW.cnt = #smeltList[uuid]
      infoW.type = 2
      helper.db.helpUuid = ""
      InnerDataManagerFW.Send2LInnerData(helper, InnerDataDefine.INNERDATA_TYPE_SMELT_SUCCESS, infoW) 
    end
  end
end
--熔炼
function smeltSuccess(human)
  if not smeltList[human._id] then
    invite(human)
  end
  local infoW = {}
  infoW.cnt = #smeltList[human._id] - 1
  infoW.type = 1
  smeltHelpSuccess(human._id)  
  sendSmeltInfo(human._id, 2)  
  smeltList[human._id] = nil
  InnerDataManagerFW.Send2LInnerData(human, InnerDataDefine.INNERDATA_TYPE_SMELT_SUCCESS, infoW)
end

function onUserLoginOut(human)
  if not smeltList[human._id] then return end
  for i=2, #smeltList[human._id] do
    local helper = Human.onlineUuid[smeltList[human._id][i].helpUuid]
    if helper then
      helper.db.helpUuid = ""      
      Broadcast.sendBroadcast(helper, Lang.DOWN, Lang.RMB_SMELT_CANCEL_SMELT)
      sendSmeltInfo(human._id, 2, human._id)
    end
  end
  smeltList[human._id] = nil
end

InnerDataManagerFW.Register(InnerDataDefine.INNERDATA_TYPE_SMELT_INVITE, invite)
InnerDataManagerFW.Register(InnerDataDefine.INNERDATA_TYPE_SMELT_HELP, helpOther)
InnerDataManagerFW.Register(InnerDataDefine.INNERDATA_TYPE_SMELT_SUCCESS, smeltSuccess)
